package com.hongxun.design.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.bean.CreateProjectBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.MyLog;

import java.util.List;

/**
 * Created by zerdoor_pc on 2016/4/29.
 */
public class ProjectDetailsAdapter extends BaseAdapter {
    private List<CreateProjectBean> beanList;
    LayoutInflater inflater;
    Activity context;
    String ss = "";

    int bluecolor;
    int graycolor;


    public ProjectDetailsAdapter(List<CreateProjectBean> beanList, Activity context) {
        this.beanList = beanList;
        this.context = context;
        bluecolor = context.getResources().getColor(R.color.project_blue);
        graycolor = context.getResources().getColor(R.color.gray);
        inflater = LayoutInflater.from(context);
    }

    public List<CreateProjectBean> getData() {
        return beanList;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        int type = Constant.CREATE_GENERAL;
        switch (beanList.get(position).getItemType()) {
            case Constant.CREATE_GENERAL:
                type = Constant.CREATE_GENERAL;
                break;
            case Constant.CREATE_REMARKS:
                type = Constant.CREATE_REMARKS;
                break;
        }
        return type;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int ViewType = getItemViewType(position);
        GeneralHolder generalHolder = null;
        RemarkslHolder remarkslHolder = null;
        if (convertView == null) {
            switch (ViewType) {
                case Constant.CREATE_GENERAL:
                    convertView = inflater.inflate(R.layout.listview_item_generl, null);
                    generalHolder = new GeneralHolder(convertView);
                    convertView.setTag(generalHolder);
                    break;
                case Constant.CREATE_REMARKS:
                    convertView = inflater.inflate(R.layout.listview_item_remarks, null);
                    remarkslHolder = new RemarkslHolder(convertView);
                    convertView.setTag(remarkslHolder);
                    break;
            }
        } else {
            switch (ViewType) {
                case Constant.CREATE_GENERAL:
                    generalHolder = (GeneralHolder) convertView.getTag();
                    break;
                case Constant.CREATE_REMARKS:
                    remarkslHolder = (RemarkslHolder) convertView.getTag();
                    break;
            }
        }
        CreateProjectBean bean = (CreateProjectBean) getItem(position);
        try {
            switch (ViewType) {
                case Constant.CREATE_GENERAL:
                    ss = "";
                    //标题类型的显隐
                    if (bean.getTypeNum() == 0) {
                        generalHolder.create_type.setText(bean.getTypeName());
                        generalHolder.create_type.setVisibility(View.VISIBLE);
                        if (position == 0) {
                            generalHolder.top_line.setVisibility(View.GONE);
                        } else {
                            generalHolder.top_line.setVisibility(View.VISIBLE);
                        }
                        generalHolder.buttom_line.setVisibility(View.VISIBLE);

                    } else {
                        generalHolder.create_type.setVisibility(View.GONE);
                        generalHolder.top_line.setVisibility(View.GONE);
                        generalHolder.buttom_line.setVisibility(View.GONE);

                    }
                    generalHolder.create_name.setText(bean.getName());//项名称
                    //判断是否为输入框
                    switch (bean.getIsWrite()) {
                        case Constant.CLICKEDTEXT:
                            setEditText(generalHolder, bean);
                            break;
                        case Constant.CLICKPOP:
                            setPop(generalHolder, bean);
                            break;
                        case Constant.CLICKACTIVITY:
                            setActivity(generalHolder, bean, position);
                            break;
                    }
                    break;
                case Constant.CREATE_REMARKS:
                    if (bean.getTypeNum() == 0) {
                        remarkslHolder.create_type.setText(bean.getTypeName());
                        remarkslHolder.create_type.setVisibility(View.VISIBLE);
                        if (position == 0) {
                            remarkslHolder.top_line.setVisibility(View.GONE);
                        } else {
                            remarkslHolder.top_line.setVisibility(View.VISIBLE);
                        }
                        remarkslHolder.buttom_line.setVisibility(View.VISIBLE);
                    } else {
                        remarkslHolder.create_type.setVisibility(View.GONE);
                        remarkslHolder.top_line.setVisibility(View.GONE);
                        remarkslHolder.buttom_line.setVisibility(View.GONE);
                    }
                    remarkslHolder.create_remarks.setText(bean.getValue());
                    remarkslHolder.create_remarks.setTextColor(graycolor);
                    break;
            }
        } catch (Exception e) {
            MyLog.L(MyApp.AppName + "适配错误：" + e.toString());
        }
        return convertView;
    }

    //设置edtext类的数据
    private void setEditText(GeneralHolder generalHolder, CreateProjectBean bean) {
        generalHolder.create_value.setVisibility(View.VISIBLE);
        if (bean.getValue() != null && bean.getValue().length() != 0) {
            generalHolder.create_value.setText(bean.getValue());
            generalHolder.create_value.setTextColor(graycolor);
        } else {
            generalHolder.create_value.setText("");

        }
    }

    private void setOtherHead(GeneralHolder generalHolder, CreateProjectBean bean) {
        generalHolder.create_value.setVisibility(View.VISIBLE);
        if (bean.getValue() != null && bean.getValue().length() == 0) {
            generalHolder.create_value.setText("");
        } else {
            generalHolder.create_value.setText(bean.getValue());
            generalHolder.create_value.setTextColor(graycolor);
        }

    }

    //设置弹出框类的数据
    private void setPop(final GeneralHolder generalHolder, final CreateProjectBean bean) {
        setOtherHead(generalHolder, bean);
        if (bean.getValuecode() != null && bean.getValuecode().length() > 0
                && bean.getValue() != null && bean.getValue().length() == 0) {

            for (SystemTypeBean code : bean.getList()) {
                if (code.getTypecode().equals(bean.getValuecode())) {
                    bean.setValue(code.getTypename());
                    generalHolder.create_value.setText(bean.getValue());
                    generalHolder.create_value.setTextColor(graycolor);
                    break;
                }
            }
        }

    }

    //设置跳转界类的数据
    private void setActivity(GeneralHolder generalHolder, final CreateProjectBean bean, int position) {
        setOtherHead(generalHolder, bean);
    }

    public class GeneralHolder {
        public TextView create_name, create_value, create_type;
        public RelativeLayout create_item_button;
        View top_line, buttom_line;

        GeneralHolder(View convertView) {
            create_item_button = (RelativeLayout) convertView.findViewById(R.id.create_item_button);
            create_name = (TextView) convertView.findViewById(R.id.create_name);
            create_value = (TextView) convertView.findViewById(R.id.create_value);
            create_type = (TextView) convertView.findViewById(R.id.create_type);
            top_line = convertView.findViewById(R.id.top_line);
            buttom_line = convertView.findViewById(R.id.buttom_line);

        }
    }

    public class RemarkslHolder {
        public TextView create_type;
        public TextView create_remarks;
        View top_line, buttom_line;

        RemarkslHolder(View convertView) {
            create_type = (TextView) convertView.findViewById(R.id.create_type);
            create_remarks = (TextView) convertView.findViewById(R.id.create_remarks);
            top_line = convertView.findViewById(R.id.top_line);
            buttom_line = convertView.findViewById(R.id.buttom_line);
        }
    }
}
